// test中间件
// 导入正则校验函数
let { nameTest, ageTest, pwdTest, nickTest } = require('../utils/regTest')


let secMid = (req, res, next) => {
  // 接受name,age参数并进行校验
  let { name, age } = req.query;
  // 不为空校验
  if (!name) return next({ code: 2, message: '参数name必须传递' });
  if (!age) return next({ code: 2, message: '参数age必须传递' });

  // 正则校验
  if (!nameTest(name)) return next({ code: 3, message: '参数name格式错误' });
  if (!ageTest(age)) return next({ code: 3, message: '参数age格式错误' });

  next();
}

let thirdMid = (req, res, next) => {

  let { name, age, pwd, nickname } = req.body;
  // 不为空校验
  if (!name) return next({ code: 2, message: '参数name必须传递' });
  if (!age) return next({ code: 2, message: '参数age必须传递' });
  if (!pwd) return next({ code: 2, message: '参数pwd必须传递' });
  if (!nickname) return next({ code: 2, message: '参数nickname必须传递' });

  // 正则校验
  if (!nameTest(name)) return next({ code: 3, message: '参数name格式错误' });
  if (!ageTest(age)) return next({ code: 3, message: '参数age格式错误' });
  if (!pwdTest(pwd)) return next({ code: 3, message: '参数pwd格式错误' });
  if (!nickTest(nickname)) return next({ code: 3, message: '参数nickname格式错误' });

  next();

}
let fourthMid = (req, res, next) => {

  let { name, age, pwd, nickname } = req.body;
  // 不为空校验

  if (!age) return next({ code: 2, message: '参数age必须传递' });
  if (!nickname) return next({ code: 2, message: '参数nickname必须传递' });

  // 正则校验
  if (!ageTest(age)) return next({ code: 3, message: '参数age格式错误' });
  if (!nickTest(nickname)) return next({ code: 3, message: '参数nickname格式错误' });

  next();

}


module.exports = {
  secMid, thirdMid, fourthMid
}